using System;
using System.IO;
using BuildBox.Core.Extensions;
using BuildBox.Core.Server.Adapters;

namespace BuildBox.Core.Server.Requests
{
    public class FileRequestHandler : RequestHandlerBase
    {
        public override void Respond(IHttpListenerContextAdapter context)
        {
            string localPath = context.Request.Url.LocalPath.Replace("/", "\\").Substring(1);
            string contentType = ContentType.TextHtml;
            string ext = Path.GetExtension(localPath);
            if (ext.Equals(".css", StringComparison.CurrentCultureIgnoreCase))
            {
                contentType = ContentType.TextCss;
            }

            if (ext.Equals(".js", StringComparison.CurrentCultureIgnoreCase))
            {
                contentType = ContentType.TextJavascript;
            }

            context.Response.WriteFile(localPath, contentType);
        }
    }
}